package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.service.VisitorService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class GetVisitorNumController {

    @Autowired
    private VisitorService visitorService;

    /**
     * 通过页面名称获取总访客量和访客量
     * @param pageName
     * @param request
     * @return
     */
    @GetMapping("/getVisitorNumByPageName")
    public String getVisitorNumByPageName(@RequestParam(value = "pageName")String pageName, HttpServletRequest request){
        try{
            int index = pageName.indexOf("/");
            if(index == -1){
                pageName = "visitorVolume";
            }
            DataMap data = visitorService.getVisitorNumByPageName(pageName,request);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("ArticleController getArticleByPage Excepion",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }
}
